package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.service.db.AnnouncementService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.vo.AnnouncementVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserCommentVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class MessageManager {

    @Reference
    private CommentService commentService;

    @Reference
    private UserInfoService userInfoService;
    @Reference
    private AnnouncementService announcementService;

    //根据操作类型查看评论列表
    public PageBeanVo findCommentList(Integer commentType, Integer pageNum, Integer pageSize) {
        //1. 调用service查询
        PageBeanVo pageBeanVo = commentService.countCommentInfo(UserHolder.get().getId(), commentType, pageNum, pageSize);
        //2. 组装返回数据
        List<Comment> commentList = (List<Comment>)pageBeanVo.getItems();
        List<UserCommentVo> userCommentVoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(commentList)){
            for (Comment comment : commentList) {
                //1. 创建commentVo
                UserCommentVo userCommentVo = new UserCommentVo();

                userCommentVo.setId(comment.getId().toHexString());
                UserInfo userInfo = userInfoService.findByUserId(comment.getUserId());
                userCommentVo.setAvatar(userInfo.getAvatar());
                userCommentVo.setNickname(userInfo.getNickname());
                userCommentVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(comment.getCreated())));

                userCommentVoList.add(userCommentVo);
            }
        }

        pageBeanVo.setItems(userCommentVoList);
        return pageBeanVo;
    }

    //公告列表
    public PageBeanVo announcementList(Integer pageNum, Integer pageSize) {
        //调用service查询
        PageBeanVo pageBeanVo = announcementService.announcementList(pageNum, pageSize);
        //得到返回的数据
        List<Announcement> announcementList = (List<Announcement>) pageBeanVo.getItems();

        ArrayList<AnnouncementVo> announcementVoList = new ArrayList();


      if(CollectionUtil.isNotEmpty(announcementList)){
          for (Announcement announcement : announcementList) {

              AnnouncementVo announcementVo = new AnnouncementVo();
              announcementVo.setId(announcement.getId().toString());
              announcementVo.setTitle(announcement.getTitle());
              announcementVo.setDescription(announcement.getDescription());
              announcementVo.setCreateDate((new SimpleDateFormat("yy-MM-dd HH:mm")).format(announcement.getCreated()));
              announcementVoList.add(announcementVo);
          }
      }
        pageBeanVo.setItems(announcementVoList);
        return pageBeanVo;
    }

}
